<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<link href="../../css/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../../js/api.js"></script>
<script type="text/javascript" src="../../js/axios.js"></script>
<script type="text/javascript" src="../../js/jquery.js"></script>
</head>

<body>
<div class="place"> <span>位置：</span>
  <ul class="placeul">
    <li>采购管理</li>
    <li>厂商管理</li>
    <li>添加</li>
  </ul>
</div>
<div class="formbody">
  <div class="formtitle"><span>厂商信息</span></div>
  <form id="form">
  <ul class="forminfo">
    <li>
      <label>厂商名称</label>
      <input name="firmName" id="firmName" type="text" class="dfinput" />
      <i class="firmTip">必填，不能超过50个字符</i>
    </li>
    <li>
      <label>负责人</label>
      <input name="firmFounder" id="firmFounder" type="text" class="dfinput" />
       <i class="founderTip">必填，不能超过30个字符</i>
    </li>
    <li>
      <label>联系方式</label>
      <input name="firmTel" id="firmTel" type="text" class="dfinput" />
       <i class="telTip">必填，不能超过30个字符</i>
    </li>
    <li>
      <label>联系地址</label>
      <input name="firmAddress" id="firmAddress" type="text" class="dfinput" />
       <i class="addressTip">必填，不能超过130个字符</i>
    </li>
    <li>
      <label>所属区域</label>
      <select class="dfselect" name="pId" id="pId">
          <option>请选择</option>
          <option>北京</option>
          <option>江苏</option>
          <option>天津</option>
      </select>
      省
      <select class="dfselect" name="firmCity" id="firmCity">
          <option>请选择</option>
          <option>北京</option>
          <option>南京</option>
          <option>天津</option>
      </select>
      市
      <i class="cityTip">必选</i>
    </li>
    <li>
      <label>描述</label>
      <textarea class="textinput" name="firmContent" id="firmContent"></textarea>
      <i>可选</i>
    </li>
    <li>
      <label>状态</label>
      <select class="dfselect" name="status" id="status">
          <option value="1">可用</option>
          <option value="2">不可用</option>
        </select>
      <i>必选</i>
    </li>
    <li>
      <label>创建人</label>
      <input name="createId" type="hidden" id="createId" value="101"/>
      <input name="createName" type="text" id="createName" value="马云" readonly="readonly" class="roinput" />
      <i>不能编辑</i>
    </li>
    <li>
      <label>创建时间</label>
      <input name="createTime" id="createTime" type="text" readonly="readonly" class="roinput" />
      <i>不能编辑</i>
    </li>
    <li>
      <label>&nbsp;</label>
      <input name="" type="submit" class="btn" value="确定"/>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <input name="" type="button" class="btn" value="返回" onclick="window.location.href='manufacturerList.html'"/>
    </li>
  </ul>
  </form>
</div>
<script type="text/javascript">
  let firmList=[]
  axiosJson(
          'get',//提交方法
          '/purchase/firmName',//提交地址
          null,
          null
  ).then((res) => {
    let audit = res.data.resultData;
    firmList=audit;
  })

  //获取当前时间
  let s = new Date().toLocaleString();
  s = s.replaceAll("/","-")
  $('#createTime').val(s);

  $(function () {

    //获取user
    $('#createId').val(localStorage.getItem('uId'));
    $('#createName').val(localStorage.getItem('username'));

    addressMenuInitOfDefault($('#pId'),$('#firmCity'),0,0)
    $('#firmName').blur(checkFirm);
    $('#firmFounder').blur(checkFounder);
    $('#firmTel').blur(checkTel);
    $('#firmAddress').blur(checkAddress);
    $('#firmCity').blur(checkCity);


    $('#form').submit(function () {
      if (checkFirm()&&checkFounder()&&checkTel()&&checkAddress()&&checkCity()){
        let firm=$('#form').serializeArray();
        axiosJson(
                'get',//提交方法
                '/purchase/addFirm',//提交地址
                firm,
                null
        ).then((res) => {
          location.href="manufacturerList.html";
        })
      }
      return false;
    })


  })

  //表单验证
  function checkFirm() {
    $('.firmTip').html("");
    let isSuccess=true;
    let firmName=$('#firmName').val();
    if (firmName==null||firmName==''){
      $('.firmTip').html("厂商名称不能为空").css("color","red");
      isSuccess=false;
    }else {
      for (let i = 0; i <firmList.length; i++) {
        if (firmName==firmList[i]){
          $('.firmTip').html("该厂商已存在").css("color","red");
          isSuccess=false;
          break;
        }
      }
    }
    return isSuccess;
  }


  function checkFounder() {
    let firmFounder=$('#firmFounder').val();
    if (firmFounder==null||firmFounder==''){
      $('.founderTip').html("负责人不能为空").css("color","red");
      return false;
    }else {
      $('.founderTip').html("");
    }
    return true;
  }
  function checkTel (){
    let isSuccess=true;
    let tel2= /^1[3456789]\d{9}$/;
    let tel=$("#firmTel").val();
    if (tel==null||tel==""){
      $(".telTip").html("联系方式不能为空").css("color","red");
      isSuccess=false;
    }else if(!tel2.test(tel)){
      $(".telTip").html("格式错误");
      isSuccess=false;
    }else {
      $(".telTip").html("");
    }
    return isSuccess;
  }
  function checkAddress() {
    let firmAddress=$('#firmAddress').val();
    if (firmAddress==null||firmAddress==''){
      $('.addressTip').html("联系地址不能为空").css("color","red");
      return false;
    }else {
      $('.addressTip').html("");
    }
    return true;
  }
  function checkCity() {
    let firmCity=$('#firmCity').val();
    if (firmCity==null||firmCity==0){
      $('.cityTip').html("请选择所属区域").css("color","red");
      return false;
    }else {
      $('.cityTip').html("");
    }
    return true;
  }



</script>
</body>
</html>
